<template>
  <div class="maintenance-record-detail">
    <!-- 页面头部 -->
    <div class="page-header">
      <div class="header-content">
        <div class="title-section">
          <h2 class="page-title">
            <i class="el-icon-view"></i>
            维护记录详情
          </h2>
          <p class="page-subtitle">查看设备维护记录的详细信息</p>
        </div>
        <div class="header-actions">
          <el-button @click="goBack" icon="el-icon-back" size="medium">
            返回列表
          </el-button>
          <el-button type="primary" @click="editRecord" icon="el-icon-edit" size="medium">
            编辑记录
          </el-button>
        </div>
      </div>
    </div>

    <!-- 主要内容区域 -->
    <div class="main-content" v-loading="loading">
      <div v-if="recordData" class="detail-content">
        <!-- 基本信息卡片 -->
        <div class="info-card">
          <div class="card-header">
            <h3 class="card-title">
              <i class="el-icon-monitor"></i>
              设备信息
            </h3>
            <el-tag :type="getStatusType(recordData.status)" size="medium">
              {{ getStatusText(recordData.status) }}
            </el-tag>
          </div>
          <div class="card-content">
            <el-row :gutter="20">
              <el-col :span="8">
                <div class="info-item">
                  <label>设备名称</label>
                  <div class="info-value">
                    <el-tag type="info" size="small">{{ recordData.equipmentName }}</el-tag>
                  </div>
                </div>
              </el-col>
              <el-col :span="8">
                <div class="info-item">
                  <label>设备编码</label>
                  <div class="info-value equipment-code">{{ recordData.equipmentCode }}</div>
                </div>
              </el-col>
              <el-col :span="8">
                <div class="info-item">
                  <label>设备位置</label>
                  <div class="info-value">{{ recordData.equipmentLocation || '--' }}</div>
                </div>
              </el-col>
            </el-row>
          </div>
        </div>

        <!-- 维护信息卡片 -->
        <div class="info-card">
          <div class="card-header">
            <h3 class="card-title">
              <i class="el-icon-tools"></i>
              维护信息
            </h3>
          </div>
          <div class="card-content">
            <el-row :gutter="20">
              <el-col :span="6">
                <div class="info-item">
                  <label>维护类型</label>
                  <div class="info-value">
                    <el-tag :type="getMaintenanceTypeColor(recordData.maintenanceType)" size="small">
                      {{ getMaintenanceTypeText(recordData.maintenanceType) }}
                    </el-tag>
                  </div>
                </div>
              </el-col>
              <el-col :span="6">
                <div class="info-item">
                  <label>维护项目</label>
                  <div class="info-value">{{ recordData.maintenanceItem }}</div>
                </div>
              </el-col>
              <el-col :span="6">
                <div class="info-item">
                  <label>维护人员</label>
                  <div class="info-value">
                    <i class="el-icon-user"></i>
                    {{ recordData.maintenancePerson }}
                  </div>
                </div>
              </el-col>
              <el-col :span="6">
                <div class="info-item">
                  <label>维护时间</label>
                  <div class="info-value">
                    <i class="el-icon-time"></i>
                    {{ formatDateTime(recordData.maintenanceTime) }}
                  </div>
                </div>
              </el-col>
            </el-row>
            
            <el-row :gutter="20">
              <el-col :span="8">
                <div class="info-item">
                  <label>维护工时</label>
                  <div class="info-value hours-text">{{ recordData.maintenanceHours }} 小时</div>
                </div>
              </el-col>
              <el-col :span="8">
                <div class="info-item">
                  <label>维护费用</label>
                  <div class="info-value cost-text">¥{{ recordData.maintenanceCost || 0 }}</div>
                </div>
              </el-col>
              <el-col :span="8">
                <div class="info-item">
                  <label>记录ID</label>
                  <div class="info-value small-text">{{ recordData.equipmentMaintenanceId }}</div>
                </div>
              </el-col>
            </el-row>
          </div>
        </div>

        <!-- 故障信息卡片 -->
        <div class="info-card" v-if="recordData.faultDescription || recordData.faultCause">
          <div class="card-header">
            <h3 class="card-title">
              <i class="el-icon-warning"></i>
              故障信息
            </h3>
          </div>
          <div class="card-content">
            <div class="info-item" v-if="recordData.faultDescription">
              <label>故障现象</label>
              <div class="info-value text-content">{{ recordData.faultDescription }}</div>
            </div>
            <div class="info-item" v-if="recordData.faultCause">
              <label>故障原因</label>
              <div class="info-value text-content">{{ recordData.faultCause }}</div>
            </div>
          </div>
        </div>

        <!-- 处理信息卡片 -->
        <div class="info-card" v-if="recordData.handlingSituation || recordData.handlingResult">
          <div class="card-header">
            <h3 class="card-title">
              <i class="el-icon-s-tools"></i>
              处理信息
            </h3>
          </div>
          <div class="card-content">
            <div class="info-item" v-if="recordData.handlingSituation">
              <label>处理方案</label>
              <div class="info-value text-content">{{ recordData.handlingSituation }}</div>
            </div>
            <div class="info-item" v-if="recordData.handlingResult">
              <label>处理结果</label>
              <div class="info-value text-content">{{ recordData.handlingResult }}</div>
            </div>
            <el-row :gutter="20" v-if="recordData.materialsUsed || recordData.nextMaintenanceDate">
              <el-col :span="12" v-if="recordData.materialsUsed">
                <div class="info-item">
                  <label>使用材料</label>
                  <div class="info-value">{{ recordData.materialsUsed }}</div>
                </div>
              </el-col>
              <el-col :span="12" v-if="recordData.nextMaintenanceDate">
                <div class="info-item">
                  <label>下次维护</label>
                  <div class="info-value">
                    <i class="el-icon-calendar"></i>
                    {{ formatDate(recordData.nextMaintenanceDate) }}
                  </div>
                </div>
              </el-col>
            </el-row>
          </div>
        </div>

        <!-- 其他信息卡片 -->
        <div class="info-card" v-if="recordData.remarks || (recordData.attachments && recordData.attachments.length > 0)">
          <div class="card-header">
            <h3 class="card-title">
              <i class="el-icon-document"></i>
              其他信息
            </h3>
          </div>
          <div class="card-content">
            <div class="info-item" v-if="recordData.remarks">
              <label>备注信息</label>
              <div class="info-value text-content">{{ recordData.remarks }}</div>
            </div>
            <div class="info-item" v-if="recordData.attachments && recordData.attachments.length > 0">
              <label>附件文件</label>
              <div class="info-value">
                <div class="attachment-list">
                  <div 
                    v-for="(attachment, index) in recordData.attachments" 
                    :key="index"
                    class="attachment-item"
                    @click="downloadAttachment(attachment)"
                  >
                    <i class="el-icon-document"></i>
                    <span>{{ attachment.fileName }}</span>
                    <el-button type="text" size="small" icon="el-icon-download">下载</el-button>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>

        <!-- 操作历史卡片 -->
        <div class="info-card">
          <div class="card-header">
            <h3 class="card-title">
              <i class="el-icon-time"></i>
              操作历史
            </h3>
          </div>
          <div class="card-content">
            <el-timeline>
              <el-timeline-item
                v-for="(item, index) in operationHistory"
                :key="index"
                :timestamp="item.timestamp"
                :type="item.type"
              >
                <div class="timeline-content">
                  <h4>{{ item.title }}</h4>
                  <p>{{ item.description }}</p>
                  <span class="operator">操作人：{{ item.operator }}</span>
                </div>
              </el-timeline-item>
            </el-timeline>
          </div>
        </div>
      </div>

      <!-- 无数据状态 -->
      <div v-else-if="!loading" class="no-data">
        <i class="el-icon-document"></i>
        <p>暂无数据</p>
      </div>
    </div>
  </div>
</template>

<script>
import { equipmentMaintenanceApi } from '@/api/equipment'

export default {
  name: 'MaintenanceRecordDetail',
  data() {
    return {
      loading: false,
      recordData: null,
      operationHistory: [
        {
          timestamp: '2024-01-15 14:30:00',
          type: 'primary',
          title: '创建维护记录',
          description: '系统自动创建维护记录',
          operator: '系统'
        },
        {
          timestamp: '2024-01-15 15:00:00',
          type: 'success',
          title: '开始维护',
          description: '维护人员开始执行维护任务',
          operator: '张三'
        },
        {
          timestamp: '2024-01-15 16:30:00',
          type: 'success',
          title: '维护完成',
          description: '维护任务已完成，设备运行正常',
          operator: '张三'
        }
      ]
    }
  },
  
  mounted() {
    this.loadRecordDetail()
  },
  
  methods: {
    // 加载记录详情
    async loadRecordDetail() {
      const recordId = this.$route.params.id
      if (!recordId) {
        this.$message.error('缺少记录ID参数')
        this.goBack()
        return
      }

      this.loading = true
      try {
        const response = await equipmentMaintenanceApi.getMaintenanceDetail(recordId)
        if (response.code === 200) {
          this.recordData = response.data || {}
        } else {
          this.$message.error(response.message || '获取详情失败')
          this.goBack()
        }
      } catch (error) {
        console.error('获取详情失败:', error)
        this.$message.error('获取详情失败')
        this.goBack()
      } finally {
        this.loading = false
      }
    },
    
    // 返回列表
    goBack() {
      this.$router.push({ name: 'EquipmentMaintenance' })
    },
    
    // 编辑记录
    editRecord() {
      if (this.recordData && this.recordData.equipmentMaintenanceId) {
        this.$router.push({
          name: 'EditMaintenanceRecord',
          params: { id: this.recordData.equipmentMaintenanceId }
        })
      }
    },
    
    // 获取状态类型
    getStatusType(status) {
      const statusMap = {
        0: 'info',      // 待处理
        1: 'success',   // 已完成
        2: 'warning',   // 进行中
        3: 'danger'     // 已取消
      }
      return statusMap[status] || 'info'
    },
    
    // 获取状态文本
    getStatusText(status) {
      const statusMap = {
        0: '待处理',
        1: '已完成',
        2: '进行中',
        3: '已取消'
      }
      return statusMap[status] || '未知'
    },
    
    // 获取维护类型颜色
    getMaintenanceTypeColor(type) {
      const typeMap = {
        'preventive': 'success',
        'repair': 'danger',
        'maintenance': 'warning',
        'emergency': 'danger',
        'upgrade': 'primary'
      }
      return typeMap[type] || 'info'
    },
    
    // 获取维护类型文本
    getMaintenanceTypeText(type) {
      const typeMap = {
        'preventive': '预防性维护',
        'repair': '故障维修',
        'maintenance': '定期保养',
        'emergency': '紧急维修',
        'upgrade': '升级改造'
      }
      return typeMap[type] || type
    },
    
    // 格式化日期时间
    formatDateTime(dateTime) {
      if (!dateTime) return '--'
      return new Date(dateTime).toLocaleString('zh-CN')
    },
    
    // 格式化日期
    formatDate(date) {
      if (!date) return '--'
      return new Date(date).toLocaleDateString('zh-CN')
    },
    
    // 下载附件
    downloadAttachment(attachment) {
      if (attachment.fileUrl) {
        window.open(attachment.fileUrl, '_blank')
      } else {
        this.$message.warning('附件链接无效')
      }
    }
  }
}
</script>

<style scoped>
.maintenance-record-detail {
  padding: 20px;
  background-color: #f5f5f5;
  min-height: 100vh;
}

.page-header {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 30px;
  border-radius: 12px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  margin-bottom: 20px;
  color: white;
}

.header-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.title-section {
  flex: 1;
}

.page-title {
  font-size: 28px;
  font-weight: 700;
  color: white;
  margin: 0 0 8px 0;
  display: flex;
  align-items: center;
  gap: 12px;
}

.page-title i {
  font-size: 32px;
  color: #ffd700;
}

.page-subtitle {
  font-size: 16px;
  color: rgba(255, 255, 255, 0.8);
  margin: 0;
  font-weight: 400;
}

.header-actions {
  display: flex;
  gap: 12px;
}

.header-actions .el-button {
  border-radius: 8px;
  transition: all 0.3s ease;
}

.header-actions .el-button:first-child {
  background: rgba(255, 255, 255, 0.2);
  border: 1px solid rgba(255, 255, 255, 0.3);
  color: white;
}

.header-actions .el-button:first-child:hover {
  background: rgba(255, 255, 255, 0.3);
  transform: translateY(-1px);
}

.main-content {
  background: white;
  border-radius: 12px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
  overflow: hidden;
  min-height: 400px;
}

.detail-content {
  padding: 30px;
}

.info-card {
  margin-bottom: 25px;
  border: 1px solid #ebeef5;
  border-radius: 8px;
  overflow: hidden;
  background: white;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}

.card-header {
  background: linear-gradient(90deg, #f8f9fa 0%, #e9ecef 100%);
  padding: 15px 20px;
  border-bottom: 1px solid #ebeef5;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.card-title {
  font-size: 18px;
  font-weight: 600;
  color: #333;
  margin: 0;
  display: flex;
  align-items: center;
  gap: 8px;
}

.card-title i {
  color: #409EFF;
  font-size: 20px;
}

.card-content {
  padding: 25px;
}

.info-item {
  margin-bottom: 20px;
}

.info-item:last-child {
  margin-bottom: 0;
}

.info-item label {
  display: block;
  font-weight: 600;
  color: #606266;
  margin-bottom: 8px;
  font-size: 14px;
}

.info-value {
  color: #303133;
  font-size: 14px;
  line-height: 1.6;
}

.equipment-code {
  font-family: 'Courier New', monospace;
  font-weight: 600;
  color: #606266;
  background: #f5f7fa;
  padding: 4px 8px;
  border-radius: 4px;
  display: inline-block;
}

.hours-text {
  color: #E6A23C;
  font-weight: 600;
  background: #fdf6ec;
  padding: 4px 8px;
  border-radius: 4px;
  display: inline-block;
}

.cost-text {
  color: #E6A23C;
  font-weight: 700;
  font-size: 16px;
}

.small-text {
  font-size: 12px;
  color: #909399;
  font-family: 'Courier New', monospace;
}

.text-content {
  color: #606266;
  line-height: 1.6;
  white-space: pre-wrap;
  word-break: break-word;
  background: #fafbfc;
  padding: 12px;
  border-radius: 6px;
  border-left: 3px solid #409EFF;
}

.attachment-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.attachment-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  background: #f8f9fa;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.attachment-item:hover {
  background: #e9ecef;
  transform: translateX(2px);
}

.attachment-item i {
  color: #409EFF;
}

.attachment-item span {
  flex: 1;
  color: #606266;
}

/* 时间线样式 */
.timeline-content h4 {
  margin: 0 0 5px 0;
  color: #303133;
  font-size: 14px;
  font-weight: 600;
}

.timeline-content p {
  margin: 0 0 5px 0;
  color: #606266;
  font-size: 13px;
  line-height: 1.4;
}

.operator {
  color: #909399;
  font-size: 12px;
}

/* 无数据状态 */
.no-data {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 60px 20px;
  color: #909399;
}

.no-data i {
  font-size: 64px;
  margin-bottom: 16px;
  color: #c0c4cc;
}

.no-data p {
  font-size: 16px;
  margin: 0;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .maintenance-record-detail {
    padding: 10px;
  }
  
  .page-header {
    padding: 20px;
  }
  
  .header-content {
    flex-direction: column;
    gap: 15px;
    text-align: center;
  }
  
  .page-title {
    font-size: 22px;
  }
  
  .header-actions {
    width: 100%;
    justify-content: center;
  }
  
  .detail-content {
    padding: 20px;
  }
  
  .card-content {
    padding: 15px;
  }
  
  .info-item {
    margin-bottom: 15px;
  }
}

@media (max-width: 480px) {
  .page-title {
    font-size: 18px;
  }
  
  .page-subtitle {
    font-size: 14px;
  }
  
  .detail-content {
    padding: 15px;
  }
  
  .card-content {
    padding: 10px;
  }
  
  .attachment-item {
    flex-direction: column;
    align-items: flex-start;
    gap: 4px;
  }
}
</style>